package com.rentplant.controller;

import com.alibaba.fastjson.JSONObject;
import com.rentplant.Pojo.user;
import com.rentplant.service.impl.userServiceImp;
import com.rentplant.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController//让它知道这是controller
@RequestMapping("/user")
public class userController {
    @Autowired
    private userServiceImp userserviceimp;

    //登录检测用户是否认证
    @RequestMapping("/login")
    public user selectUser(@RequestBody JSONObject req){
        try {
            String openid = req.getString("openid");
//            System.out.println(openid);
            List<user> userArray = userserviceimp.userInfo(openid);
//            System.out.println(userArray.get(0).getInfostate());
            user infostate = userArray.get(0);
            return infostate;
        }catch (Exception e){
            return null;
        }
    }

    //提取用户个人信息
    @RequestMapping("/personInfo")
    public List<user> selectUserInfo(@RequestBody JSONObject req){
        try {
            String openid = req.getString("openid");
//        System.out.println(userserviceimp.userInfo(openid));
            return userserviceimp.userInfo(openid);
        }catch (Exception e){
           return null;
        }

    }

    //修改个人信息
    @RequestMapping("/updateInfo")
    public boolean updateInfo(@RequestBody JSONObject req){
        return userserviceimp.updateInfo(req);
    }

    //添加用户信息
    @RequestMapping("/addUser")
    public boolean addUser(@RequestBody JSONObject req){
        System.out.println(req);
        return userserviceimp.addUser(req);
    }

    //充值余额
    @RequestMapping("/updateMoney")
    public boolean updateMoney(@RequestBody JSONObject req){
//        System.out.println(req);
        return userserviceimp.updateMoney(req);
    }

//    更新微信用户信息
    @RequestMapping("/updateWxInfo")
    public boolean updateWxInfo(@RequestBody JSONObject req){
//        System.out.println(req);
        return userserviceimp.updateWxInfo(req);
    }






    //    管理端接口
//   查询全部用户
    @RequestMapping("/allUser")
    public List<user> allUser(@RequestBody JSONObject req){
        try {
            return userserviceimp.allUser(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    @RequestMapping("/allUsers")
    //    查询全部用户
    public List<user> allUsers(){
        try {
            return userserviceimp.allUsers();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    更改用户信息
    @RequestMapping("/updateUserInfo")
    public boolean updateUserInfo(@RequestBody JSONObject req){
        try{
//            System.out.println(req);
            return userserviceimp.updateUserInfo(req);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }

    }

    //    按条件查询
    @RequestMapping("/partUser")
    public List<user> partUser(@RequestBody JSONObject req){
        try {
            System.out.println(userserviceimp.partUser(req));
            return userserviceimp.partUser(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    按条件查询
    @RequestMapping("/partUsers")
    public List<user> partUsers(@RequestBody JSONObject req){
        try {
            System.out.println(userserviceimp.partUsers(req));
            return userserviceimp.partUsers(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }











}


